/**
 * 详情对话框
 */
var StoragePutSingleInfoDlg = {
    data: {
    	positionNumber: "",
    }
};
/**
 * 添加或者修改页面
 */
var OrderPutDetailedInfoDlg = {
    data: {
        detailedGoodsname: "",
        detailedGoodsnum: "",
        goodsNumber: "",
		goodsNo:"",
        detailedRebubbles: "",
        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        detailedPutcondition: "",
        detailedPiecenum: "",
        goodsWeight: "",
        goodsVolume: "",
    }
};
layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    /**
     * 库存表管理
     */
    var StorageStock = {
        tableId: "storageStockTable"
    };

    /**
     * 初始化表格的列
     */
    StorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'operateName', title: '项目'},
            {field: 'customerName', title: '客户'},
            {field: 'warehouseName', title: '库房'},
            {field: 'stockstate', title: '状态',templet: '#stockstateTpl'},
            {field: 'areaNumber', title: '库区'},
            {field: 'positionNumber', title: '库位', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'trayNumber', title: '托盘号', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsName', title: '商品', style:'background-color: #dbf1e1;font-weight: 600;',width:270},
            {field: 'goodsNumber', title: '编号', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', title: '货号', style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: '合计：'},
            {field: 'num', title: '数量', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            /*{field: 'storageNumber', title: '待拣货数', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},*/
            {field: 'stockWeight', title: '重量', totalRow: true},
            {field: 'stockVolume', title: '体积', totalRow: true},
            {field: 'stockMoney', title: '金额', totalRow: true},
            {field: 'unitprice', title: '单价'},
            {field: 'putTime', title: '入库批次'},
            {field: 'producetime', title: '生产日期'},
            {field: 'goodsbatchs', title: '批次'},
            {field: 'goodsPacking', title: '包装'},
            {field: 'goodsSpecs', title: '规格'},
            {field: 'goodsWarranty', title: '保质期(天)'},
            {field: 'weidao3', title: '未到3/1期',align: 'center',
				templet: function (d) {
            		var str = "";
            		if (d.weidao3 == '1') {
                    	str += "<i class='layui-icon' style='color:green;font-weight: 600;'>√</i>"
					} 
            		return str
				}
			},
            {field: 'chao3', title: '超3/1期',align: 'center',
				templet: function (d) {
            		var str = "";
            		if (d.chao3 == '1') {
                    	str += "<i class='layui-icon' style='color:green;font-weight: 600;'>√</i>"
					} 
            		return str
				}
			},
            {field: 'chao2', title: '超2/1期',align: 'center',
				templet: function (d) {
            		var str = "";
            		if (d.chao2 == '1') {
                    	str += "<i class='layui-icon' style='color:green;font-weight: 600;'>√</i>"
					} 
            		return str
				}
			},
            {field: 'guoqi', title: '过期',align: 'center',
				templet: function (d) {
            		var str = "";
            		if (d.guoqi == '1') {
                    	str += "<i class='layui-icon' style='color:red;font-weight: 600;'>√</i>"
					} 
            		return str
				}
			},
            {field: 'yiguo22', title: '保质期已超22个月',align: 'center',
				templet: function (d) {
            		var str = "";
            		if (d.yiguo22 == '1') {
                    	str += "<i class='layui-icon' style='color:red;font-weight: 600;'>√</i>"
					} 
            		return str
				}
			},
            {field: 'kuling', title: '库龄(天)'},
            {field: 'youxianqi', title: '有效期'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStock.search = function () {
        var queryData = {};
        queryData['stockstate'] = $("#stockstate").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        table.reload(StorageStock.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    /**
     * 导出excel按钮
     */
    StorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
			console.log(checkRows.data)
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStock.tableId,
        url: Feng.ctxPath + '/storageStock/findshishikucun',
        page: false,
        limit: 1000,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
		defaultToolbar: ['filter'],
        totalRow: true ,//开启合计行
        cols: StorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
    	StorageStock.search();
    });

    // 单击行事件
    table.on('row(' + StorageStock.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StorageStock.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StorageStock.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });

    // 导出excel
    $('#btnExp').click(function () {
    	StorageStock.exportExcel();
    });
    
    //点击选择商品名称
    $('#choiceg').click(function () {
    	var customerId = $("#customerId").val();
    	if (customerId==null||customerId=='') {
    		layer.msg('Please Select Customer'); 
		} else {
	        layer.open({
	            type: 2,
	            title: '选择商品',
	            area: ['900px', '500px'],
	            content: Feng.ctxPath + '/orderPutDetailed/toChoiceGoods?goodsCustomerId=' + customerId,
	            end: function () {
	                $("#goodsName").val(OrderPutDetailedInfoDlg.data.detailedGoodsname);//商品名称
	                $("#goodsNumber").val(OrderPutDetailedInfoDlg.data.goodsNumber);//商品编号
	                $("#goodsNo").val(OrderPutDetailedInfoDlg.data.goodsNumber);
	            }
	        });
		}
    });
    
    //查询商品类别
    form.on('select(goodsBigtype)',function(){
    	$("#goodsType").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/storageStock/queryGoodsType',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"dictcode":$("#goodsBigtype").find("option:checked").attr("id"),"code":"COMMODITY_CATEGORY"},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>请选择</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.name+"'>"+value.name+"</option>";
    	        	});
    	        	$("#goodsType").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });

});
